<template>
	<div class="table-demo-container layout-padding">
		<div class="table-demo-padding layout-padding-view layout-padding-auto">
			<TableSearch :search="state.tableData.search" @search="onSearch" />
			<Table
				ref="tableRef"
				v-bind="state.tableData"
				class="table-demo"
				@delRow="onTableDelRow"
				@pageChange="onTablePageChange"
				@sortHeader="onSortHeader"
			/>
		</div>
	</div>
</template>

<script setup lang="ts" name="studentManagement">
import { defineAsyncComponent, reactive, ref, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import { studentInfo, delStudentInfo, getStudentInfo } from '/@/api/student/management';
import { decrypt } from '/@/utils/RSA';

// 引入组件
const Table = defineAsyncComponent(() => import('/@/components/table/index.vue'));
const TableSearch = defineAsyncComponent(() => import('/@/views/student/management/search.vue'));

// 定义变量内容
const tableRef = ref<RefType>();
const state = reactive<TableDemoState>({
	tableData: {
		// 列表数据（必传）
		data: [],
		// 表头内容（必传，注意格式）
		header: [
			{ key: 'id', colWidth: '', title: '学号', type: 'text', isCheck: true },
			{ key: 'name', colWidth: '', title: '学生姓名', type: 'text', isCheck: true },
			{ key: 'class', colWidth: '', title: '班级', type: 'text', isCheck: true },
			{ key: 'phone', colWidth: '', title: '联系电话', type: 'text', isCheck: true },
		],
		// 配置项（必传）
		config: {
			total: 0, // 列表总数
			loading: true, // loading 加载
			isBorder: false, // 是否显示表格边框
			isSerialNo: true, // 是否显示表格序号
			isSelection: true, // 是否显示表格多选
			isOperate: true, // 是否显示表格操作栏
		},
		// 搜索表单，动态生成（传空数组时，将不显示搜索，注意格式）
		search: [
			{ label: '学号', prop: 'id', placeholder: '请输入想查询的学生学号', required: false, type: 'input' },
			{ label: '学生姓名', prop: 'name', placeholder: '请输入想查询的学生姓名', required: false, type: 'input' },
			{ label: '班级', prop: 'class', placeholder: '请输入想查询的学生班级', required: false, type: 'input' },
			{ label: '联系电话', prop: 'phone', placeholder: '请输入想查询的学生的联系电话', required: false, type: 'input' },
		],
		// 搜索参数（不用传，用于分页、搜索时传给后台的值，`getTableData` 中使用）
		param: {
			pageNum: 1,
			pageSize: 10,
		},
		// 打印标题
		printName: '学生信息打印',
	},
});

// 初始化列表数据
const getTableData = () => {
	state.tableData.config.loading = true;
	state.tableData.data = [];
	studentInfo(state.tableData.param.pageNum)
		.then((res) => {
			if (res.code === '2000') {
				state.tableData.data = res.data.map((item: any) => {
					return {
						id: item.student_id,
						name: item.student_name,
						class: item.lesson_name,
						phone: decrypt(item.phone),
					};
				});
				state.tableData.config.total = res.totalCount;
			}
		})
		.catch(() => {
			ElMessage.error('请求数据失败');
		});
	setTimeout(() => {
		state.tableData.config.loading = false;
	}, 500);
};
// 搜索点击时表单回调
const onSearch = (data: EmptyObjectType) => {
	state.tableData.param = Object.assign({}, state.tableData.param, { ...data });
	const params = {
		Page: state.tableData.param.pageNum,
		student_id: state.tableData.param.id === '' ? '' : state.tableData.param.id,
		student_name: state.tableData.param.name === '' ? '' : state.tableData.param.name,
		lesson_name: state.tableData.param.class === '' ? '' : state.tableData.param.class,
		phone: state.tableData.param.phone === '' ? '' : state.tableData.param.phone,
	};
	getStudentInfo(params)
		.then((res) => {
			if (res.code === '2000') {
				state.tableData.data = res.data.map((item: any) => {
					return {
						id: item.student_id,
						name: item.student_name,
						class: item.lesson_name,
						phone: decrypt(item.phone),
					};
				});
				state.tableData.config.total = res.totalCount;
			}
		})
		.catch(() => {
			ElMessage.error('请求数据失败');
		});
};
// 删除当前项回调
const onTableDelRow = (row: EmptyObjectType) => {
	delStudentInfo({ student_id: row.id })
		.then((res) => {
			if (res.code === '2000') {
				ElMessage.success(`删除${row.name}成功！`);
				getTableData();
			} else {
				ElMessage.error('删除失败');
			}
		})
		.catch((err) => {
			ElMessage.error('删除数据失败!' + err);
		});
};
// 分页改变时回调
const onTablePageChange = (page: TableDemoPageType) => {
	state.tableData.param.pageNum = page.pageNum;
	state.tableData.param.pageSize = page.pageSize;
	getTableData();
};
// 拖动显示列排序回调
const onSortHeader = (data: TableHeaderType[]) => {
	state.tableData.header = data;
};
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>

<style scoped lang="scss">
.table-demo-container {
	.table-demo-padding {
		padding: 15px;
		.table-demo {
			flex: 1;
			overflow: hidden;
		}
	}
}
</style>
